package handler

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"math/rand"
	"net/http"
	"project/api/params"
	"project/golbal"
	"project/inits"
	"project/model"
	"project/pkg"
	"strconv"
	"time"
)

func Login(c *gin.Context) {

	req := params.UserReq{}
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
		return
	}

	var user model.User

	golbal.DB.Debug().Where("mobile", user.Mobile).Find(&user)

	handler, _ := pkg.TokenHandler(strconv.Itoa(int(user.ID)))

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "登录成功",
		"data": handler,
	})

}

func Send(c *gin.Context) {
	sms := params.Sms{}
	code := rand.Intn(9000) + 1000

	lod := fmt.Sprintf("%d:短信", sms.Mobile)
	golbal.Rdb.Set(inits.Ctx, strconv.Itoa(code), lod, time.Minute*3)
	sendSms, err := pkg.SendSms(sms.Mobile, strconv.Itoa(code))
	if err != nil {
		return
	}
	if *sendSms.Body.Code != "OK" {
		c.JSON(http.StatusBadRequest, gin.H{

			"code": 500,
			"msg":  *sendSms.Body.Message,
		})

	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "短信发送成功",
	})

}
func Register(c *gin.Context) {

}

func FriendShow(c *gin.Context) {

}

func FriendAdd(c *gin.Context) {

}

func FriendDelete(c *gin.Context) {

}

func Chat(c *gin.Context) {

}

func Create(c *gin.Context) {

}
